package com.woniuxy.company5.controller;

import com.woniuxy.company5.dto.AddWaitRep;
import com.woniuxy.company5.dto.FenpeiRepDto;
import com.woniuxy.company5.entity.*;
import com.woniuxy.company5.service.*;
import com.woniuxy.company5.util.Result;
import com.woniuxy.company5.vo.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/superReps")
public class RepMangerController {
    @Resource
    private RepProblemListService repProblemListService;
    @Resource
    private RepGoodlistsService repGoodlistsService;
    @Resource
    private RepNewService repNewService;
    @Resource
    private RepWaitService repWaitService;
    @Resource
    private UserEmpService userEmpService;
    @Resource
    private UserOutService userOutService;
    @Resource
    private RepDistributionService repDistributionService;
    @Resource
    private RepRefundService repRefundService;
    @Resource
    private RepProgressService repProgressService;
    @Resource
    private RepFeedbackService repFeedbackService;



    //查询所有新工单
    @GetMapping("showAllNeps")
    public Result showAllNeps(){
        List<RepNew> repNews = repNewService.selAllRepNew();
        return Result.ok().data("allNewReps",repNews);
    }
    //查询所有已分配工单
    @GetMapping("showRepDir")
    public Result showRepDir(){
        List<RepDistributionVo> repDistributions = repDistributionService.selAllDirRep();
        return Result.ok().data("showRepDir",repDistributions);
    }
    //查询所有退款工单
    @GetMapping("showRefundRep")
    public Result showRefundRep(){
        List<RepRefundVo> repRefundVos = repRefundService.selAllRefundRep();
        return Result.ok().data("showRefundReps",repRefundVos);
    }
    //查询所有完成工单
    @GetMapping("showGoodRep")
    public Result showGoodRep(){
        List<RepGoodlists> repGoodlists = repGoodlistsService.selAllGoodReps();
        return Result.ok().data("showGoodReps",repGoodlists);
    }

    //查询所有工单进度
    @GetMapping("showRepsProgress")
    public Result showRepsProgress(){
        List<RepProgressVo> repProgresses = repProgressService.selAllReps();
        return Result.ok().data("RepProgressData",repProgresses);
    }

    //查询所有反馈工单
    @GetMapping("showRepFeedback")
    public Result showRepFeedback(){
        List<RepFeedbackVo> repFeedbackVos = repFeedbackService.selAllRepFeedback();
        return Result.ok().data("showRepFeedbackData",repFeedbackVos);
    }
    //查询所有待分配工单通过num
    @GetMapping("searchByNums/{RepNums}")
    public Result searchByNums(@PathVariable String RepNums){
        RepWait repWait = repWaitService.selWaitRepByNum(RepNums);
        return Result.ok().data("repWaitsByNum",repWait);
    }

    //查询所有进度工单表工单通过num
    @GetMapping("proSearchByNums/{RepNums}")
    public Result proSearchByNums(@PathVariable String RepNums){
        List<RepProgressVo> repProgressVos = repProgressService.selByNum(RepNums);
        return Result.ok().data("ProRepsByNum",repProgressVos);
    }

    //查询所有反馈工单表工单通过num
    @GetMapping("FeedbSearchByNums/{RepNums}")
    public Result FeedbSearchByNums(@PathVariable String RepNums){
        List<RepFeedbackVo> repFeedbackVos = repFeedbackService.FeedbSearchByNums(RepNums);
        return Result.ok().data("FeedbSearchByNums",repFeedbackVos);
    }

    //查询所有分配工单表工单通过num
    @GetMapping("disSearchByNums/{RepNums}")
    public Result disSearchByNums(@PathVariable String RepNums){
        List<RepDistributionVo> repDistributionVos = repDistributionService.disSearchByNums(RepNums);
        return Result.ok().data("disSearchByNums",repDistributionVos);
    }

//    //查询所有提交工单表工单通过num和type
//    @GetMapping("searchCommitByNums/{RepNums}{changeType}")
//    public Result searchCommitByNums(@PathVariable String RepNums,@PathVariable String changeType){
//        if(changeType.equals("问题工单")){
//            List<RepRefundVo> repRefundVos = repRefundService.selRefundRepByNum(RepNums);
//            return Result.ok().data("showRefundReps",repRefundVos);
//        }else if(changeType.equals("完成工单")){
//            List<RepGoodlists> repGoodlists = repGoodlistsService.selGoodRepsByNum(RepNums);
//            return Result.ok().data("showGoodReps",repGoodlists);
//        }else {
//            return Result.error();
//        }
//    }

    //查询所有待分配工单
    @GetMapping("showWaitReps")
    public Result showWaitReps(){
        List<RepWait> repWaits = repWaitService.selAllWaitReps();
        return Result.ok().data("repWaits",repWaits);
    }

    //从新工单拿出，放入待分配工单
    @Transactional
    @PostMapping("addWaitNeps")
    public Result addWaitNeps(@RequestBody AddWaitRep addWaitRep){
        RepNew repNew = repNewService.selByNum(addWaitRep.getNum());
        Long id = new Long((long)repNew.getId());
        int i = repNewService.updateByNum(addWaitRep.getNum());
        if(i<0){
            return Result.error();
        }else{
            RepWait repWait = new RepWait();
            repWait.setDate(new Date());
            repWait.setMoney(repNew.getMoney());
            repWait.setType(1);
            repWait.setNeed(addWaitRep.getNeed());
             repWait.setNum(repNew.getNum());
            int insert = repWaitService.insert(repWait);
            if(insert<0){
                return Result.error();
            }
        }
        return Result.ok();
    }

    //改变反馈工单审核状态,放入退款工单或者完成工单
    @Transactional
    @PostMapping("submitRepFeedback")
    public Result submitRepFeedback(@RequestBody RepFeedbackVo repFeedback){
        RepNew repNew = repNewService.selByNum(repFeedback.getNum());
        int i=-1;
             if(repFeedback.getAudit()==1){
                 return Result.error().data("mes","已通过审核");
             }else{
                 repFeedback.setAudit(1);
                i = repFeedbackService.updateAuditByNum(repFeedback);
                if(repFeedback.getSuccessful()==1){
                    RepGoodlists repGoodlists = new RepGoodlists();
                    repGoodlists.setCustomerId(repFeedback.getCustomerId());
                    repGoodlists.setEngineerId(repFeedback.getEngineerId());
                    repGoodlists.setNum(repFeedback.getNum());
                    repGoodlists.setMoney(repNew.getMoney());
                    repGoodlists.setDockingId(repNew.getDockingId());
                    repGoodlists.setEndDate(new Date());
                    repGoodlists.setTimeout(2);
                    repGoodlists.setFinancialState(1);
                     repGoodlistsService.insert(repGoodlists);
                }else{
                    RepProblemList repProblemList = new RepProblemList();
                    repProblemList.setNum(repFeedback.getNum());
                    repProblemList.setEngineerId(repFeedback.getEngineerId());
                    repProblemList.setProblemId(repFeedback.getProblemId());
                    repProblemList.setCustomerId(repFeedback.getCustomerId());
                    repProblemList.setDesc1(repFeedback.getDesc1());
                    repProblemListService.insert(repProblemList);
                }
             }
        repFeedbackService.deleteByNum(repFeedback.getNum());
        if(i<0){
            return Result.error();
        }
        return Result.ok();

    }


    //改变工单Need
    @PostMapping("changeNeed")
    public Result changeNeed(@RequestBody RepWait repWait1){
        RepWait repWait = repWaitService.selWaitRepByNum(repWait1.getNum());
        if(repWait1.getNeed()==1){
            repWait.setNeed(2);
        }else{
            repWait.setNeed(1);
        }
        int i = repWaitService.updateByNum(repWait);
        if(i<0){
            return Result.error();
        }else{
            return Result.ok();
        }
    }

    //分配工单，从待分配工单取出，放入分配工单
    @Transactional
    @PostMapping("distributionForm")
    public Result distributionForm(@RequestBody FenpeiRepDto fenpeiRepDto){
        System.out.println("进来了1"+fenpeiRepDto);
        int i1=0;
        if(fenpeiRepDto!=null){
            System.out.println("进来了2");
            RepWait repWait = repWaitService.selWaitRepByNum(fenpeiRepDto.getNum());
            RepDistribution repDistribution = new RepDistribution();
            repDistribution.setNum(repWait.getNum());
            repDistribution.setDeadline(fenpeiRepDto.getDeadline());
            UserEmp userEmp = userEmpService.selectByName(fenpeiRepDto.getEngineerName());
            repDistribution.setEngineerId(userEmp.getId());
            repDistribution.setStart(new Date());
            if(fenpeiRepDto.getOutName()!=""){
                System.out.println("进来了outman");
                UserOut userOut = userOutService.selectByName(fenpeiRepDto.getOutName());
                repDistribution.setOutId1(userOut.getId());
            }
             i1 = repDistributionService.insertSelective(repDistribution);
            int i = repWaitService.deleteByPrimaryKey(Long.valueOf(repWait.getId()));
        }
        if(i1<0){
            return Result.error();
        }else{
            return Result.ok();
        }
    }


    //取出等待工单，加入退款工单（）
    @PostMapping("refundRep")
    public Result refundRep(@RequestBody RepFeedback repFeedback){
        RepNew repNew = repNewService.selByNum(repFeedback.getNum());
        RepWait repWait = new RepWait();
        repWait.setNum(repFeedback.getNum());
         repWait.setType(2);
         repWait.setDate(new Date());
         repWait.setNeed(2);
         repWait.setMoney(repNew.getMoney());
         repWaitService.insertSelective(repWait);
        return Result.ok();
    }


    //取出反馈工单，改type为2加入待分配工单
    @Transactional
    @PostMapping("refreshReps")
    public Result refundRep(@RequestBody RepWait repWait1){
        RepWait repWait = repWaitService.selWaitRepByNum(repWait1.getNum());
        int i1 = repWaitService.deleteByPrimaryKey(Long.valueOf(repWait.getId()));
        RepRefund repRefund = new RepRefund();
        repRefund.setNum(repWait.getNum());
        repRefund.setMoney(repWait.getMoney());
        //通过工单编号取到客户ID
        int i = repRefundService.insertSelective(repRefund);
        if(i<0){
            return Result.error();
        }else{
            return Result.ok();
        }
    }

    //分类查询待分配工单
    @GetMapping("selWaitRepByType/{changeType}")
    public Result selWaitRepByType(@PathVariable String changeType){
        if(changeType.equals("0")){
            List<RepWait> repWaits = repWaitService.selAllWaitReps();
            return Result.ok().data("selWaitRepByType",repWaits);
        }else{
            List<RepWait> repWaits = repWaitService.selWaitRepByType(changeType);
            return Result.ok().data("selWaitRepByType",repWaits);
        }
    }
}
